<?php
namespace Admin\Service;

use Think\Service\BasicService;

class OrderService extends BasicService {
    public function insert($model) {
        return $this->db->add($model);
    }

    public function AddAll($model) {
        $data = $this->db->addAll($model);
        return $data;
    }

    public function remove($condition) {
        return $this->db->where($condition)->delete();
    }

    public function update($where, $model = array()) {
        return $this->db->where($where)->save($model);
    }

    public function find($condition) {
        return $this->db->where($condition)->find();
    }

    public function query($condition, $limit = array(), $field = '*', $order = '', $join1 = '',$join2='') {
        $data = $this->db
            ->join($join1)
            ->join($join2)
            ->where($condition)->field($field)->limit($limit['start'], $limit['end'])->order($order)->select();
        foreach ($data as $k => $v) {
            $order_book = $this->GetOrderBook($v['order_id']);
            $data[$k]['order_books'] = $order_book;
            $ORDER_STATE_SHOW = C('ORDER_STATE_SHOW');
            $data[$k]['StateShow'] = $ORDER_STATE_SHOW[$v['order_type']];
        }
        return $data;
    }
    public function query1($condition, $limit = array(), $field = '*', $order = '', $join = '') {
        $data = $this->db->join($join)->where($condition)->field($field)->limit($limit['start'], $limit['end'])->order($order)->select();
        return $data;
    }
    public function exSql($sql){
        $Model = new D();//或者 $Model = D(); 或者 $Model = M();
        $data = $Model->query($sql);
        return $data;
    }

    public function count($condition) {
        return $this->db->where($condition)->count();
    }

    public function GetOrderBook($OID) {
        $this->db = M('order_book');
        $data = $this->db->join('ts_order on ts_order.order_id = ts_order_book.order_id')//            ->join('ts_book on ts_book.ID = ts_order_book.book_id')
        ->field('ts_order_book.*')->where(array('ts_order.order_id' => $OID))->select();
        return $data;
    }

    public function GetWZ($ID) {
        $data = $this->db->field('ts_ck.*,ts_sj.CODE as SJCODE,ts_sg.CODE as SGCODE')->join('ts_sg  on ts_sg.BHCODE = ts_book.WZ_CODE')->join('ts_sj on ts_sg.SJ_ID = ts_sj.BHCODE')->join('ts_ck on ts_ck.BHCODE = ts_sj.CK_ID')->where(array('ts_book.ID' => $ID))->find();
        $data ? $str = $data['P'] . $data['C'] . $data['Z'] . $data['ADDRESS'] . $data['NAME'] . '书架代码:' . $data['SJCODE'] . '书柜代码:' . $data['SGCODE'] : $str = '书籍未入库';
        return $str;
    }

    /*
     * 修改书籍内图
     */
    public function UpdatePic($ID, $save) {
        $this->db->startTrans();
        $OK = $this->db->where(array('BOOK_ID' => $ID))->select();
        $DELOK = 1;
        if ($OK) {
            $DELOK = $this->db->where(array('BOOK_ID' => $ID))->delete();
        }
        if ($DELOK) {
            $ADDOK = $this->db->addAll($save);
            if ($ADDOK) {
                $this->db->commit();
                return 1;
            }
        }
        $this->db->rollback();
        return 0;
    }

    /*
     * 卖书书籍出库
     * 库存不减少
     * 位置移除
     */
    public function bookck($wz_id,$ob_id,$user){
        $this->db->startTrans();
        $wzdata  = M('book_wz_view')->where(array('w_ID'=>$wz_id))->find();
        $DOK = $this->db->where(array('ID'=>$wz_id))->delete();
        if($DOK){
            $this->db= M('order_book');
            $where1['ob_id'] = $ob_id;
            $save1['state'] =1;
            $save1['info'] ='由账号'.$user.'操作出库,出库位置：'.$wzdata['c_P'].$wzdata['c_C'].$wzdata['c_Z'].$wzdata['c_ADDRESS'].$wzdata['c_NAME'].$wzdata['j_CODE'].$wzdata['g_CODE'];
            $OK = $this->db->where($where1)->save($save1);
            if($OK){
                $this->db->commit();
                return 1;
            }
        }
        $this->db->rollback();
        return 0;

    }

    //拆分订单
    public function BreakUpOrder($old_order_id,$order,$order_book){
        $this->db->startTrans();
        $OK1 =$this->db->where(array('order_id'=>$old_order_id))->save(array('order_type'=>-1));
        if($OK1){
            $OK2 =$this->db->addAll($order);
            if($OK2){
                $this->db = M('order_book');
                $OK3 =$this->db->addAll($order_book);
                if($OK3){
                    $this->db->commit();
                    return 1;
                }
            }

        }
        $this->db->rollback();
        return 0;
    }

    //待发货订单自动挂卖书寄回书籍
    public function BuyOrderAddHsBook($save,$where1,$save1){
        $this->db->startTrans();
        $OK = $this->db->add($save);
        if($OK){
            $this->db = M('next_order');
            $OK1 = $this->db->where($where1)->save($save1);
            if($OK1){
                $this->db->commit();
                return 1;
            }

        }
        $this->db->rollback();
        return 0;

    }

}


?>